package view.admin;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import controllers.*;
import entity.RoomType;
import constants.*;
import javax.swing.*;
public class TypeOperate extends JDialog{
	
	private JTextField tfTypeName 	= new JTextField(10);
	private JTextField tfTypeCost 	= new JTextField(10);
	
	private JButton	btAddType	= new JButton("Xác nhận");
	private JButton btCancel	= new JButton("Hủy bỏ");
	
	private RoomType 	roomType = null;
	private boolean 	resultOk = false;
	private int index;
	public TypeOperate(JDialog parent, String title, RoomType roomType, int index){
		super(parent, title, true);
		this.index=index;
		if (this.index==-1)
			this.roomType = new RoomType();
                
		else{
			this.roomType = roomType;
			tfTypeName.setText(roomType.getName());
			tfTypeCost.setText(new Integer(roomType.getCost()).toString());
		}
		
		setSize(450, 200);
		setLocationRelativeTo(parent);
		setResizable(false);
		
		initUI();
	}
	
	private void initUI(){
		setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(10, 20, 10, 20);
		gbc.fill = GridBagConstraints.HORIZONTAL;
		
		gbc.gridy = 0; gbc.gridx = 0;  	add(new JLabel("Tên loại phòng"), gbc);
		gbc.gridy = 1; gbc.gridx = 0;	add(new JLabel("Giá loại phòng"), gbc);
		
		gbc.weightx = 1.0;
		gbc.gridy = 0; gbc.gridx = 1;	add(tfTypeName, gbc);
		gbc.gridy = 1; gbc.gridx = 1;	add(tfTypeCost, gbc);
		gbc.weightx = 0;
		
		gbc.gridy = 0; gbc.gridx  = 2; 	add(btAddType, gbc);
		gbc.gridy = 1;	gbc.gridx = 2; 	add(btCancel, gbc);
		
		btAddType.addActionListener(new ButtonHandler());
		btCancel.addActionListener(new ButtonHandler());
	}
	
	public boolean getResult(){
		setVisible(true);
		return resultOk;
	}
	public RoomType getRoomType(){
                return roomType;
        }
	class ButtonHandler implements ActionListener{
		public void actionPerformed(ActionEvent event){
			if (event.getSource() == btAddType){
				if (!Validate.isEmpty(tfTypeName.getText()) || 
					(!Validate.isMoney(tfTypeCost.getText()))) {		
				
                                 JOptionPane.showMessageDialog(null,Validate.message);
                                }else{
                                    roomType.setName(tfTypeName.getText());
                                    roomType.setCost(new Integer(tfTypeCost.getText()));
                                    if(index!=-1)
                                        RoomController.updateType(index, roomType);
                                    else
                                        RoomController.insertType(roomType);                                                                            
                                     resultOk = true;
                                     dispose();
                                }
			}else if (event.getSource() == btCancel){
				dispose();
			} 
		}
	}
}
